//
//  TPPhotoBrowserViewCell.m
//  美丽说图片浏览器
//
//  Created by 唐鹏 on 16/7/14.
//  Copyright © 2016年 tangpeng. All rights reserved.
//

#import "TPPhotoBrowserViewCell.h"
#import "TPHomeCellItem.h"

@interface TPPhotoBrowserViewCell()

/***/
@property (nonatomic,strong) UIImage *image;


@end

@implementation TPPhotoBrowserViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        UIImageView *imageView = [[UIImageView alloc] init];
        self.imageView = imageView;
        [self.contentView addSubview:imageView];
    }
    return self;
}

- (void)setItem:(TPHomeCellItem *)item{
    _item = item;
    UIImage *image = [[SDWebImageManager sharedManager].imageCache imageFromMemoryCacheForKey:item.q_pic_url];
    if (image == nil) {
        image = [UIImage imageNamed:@"empty_picture"];
    }
    self.imageView.frame = [self getImageViewFrame:image];
    
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:item.z_pic_url] placeholderImage:[UIImage imageNamed:@"empty_picture"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        [self getImageViewFrame:image];
    }];
    
    
}

- (CGRect)getImageViewFrame:(UIImage *)image{
    CGFloat imageViewW = tp_screenWidth;
    CGFloat imageViewH = tp_screenWidth * image.size.height / image.size.width;
    CGFloat x = 0;
    CGFloat y = (tp_screenHeight - imageViewH) * 0.5;
    return CGRectMake(x, y, imageViewW, imageViewH);
}

//- (void)layoutSubviews
//{
//    [super layoutSubviews];
//    
////    self.imageView.frame =
//}

@end
